package com.canbot.u05.activity.shoppingGuide.sales;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.activity.customvoice.ChooseAudioActivity;
import com.canbot.u05.activity.shoppingGuide.ShoppingChooseAudioActivity;
import com.canbot.u05.utils.ToastTools;

/**
 * 导购模式售卖宣传语音设置界面
 */
public class SalesQuestionVoiceActivity extends BaseActivityNoTitle implements View.OnClickListener {

        private RelativeLayout rlLocalSpeech;

        private RelativeLayout rlTextToSpeech;

        private Button btnBack, btnFinish;

        private String existPath;   //已选音频路径

        private String ttsResult;   //tts语音

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_sales_question_voice);
//        MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND,PathConst.SHOPPING_GUIDE_DIR+"sale/rwbjdr_cxlk_u05_001.wav");
                setViews();
        }

        private void setViews() {
                btnBack = (Button) findViewById(R.id.btn_back);
                btnFinish = (Button) findViewById(R.id.btn_complete);
                btnBack.setOnClickListener(this);
                btnFinish.setOnClickListener(this);

                rlLocalSpeech = (RelativeLayout) findViewById(R.id.rl_local_speech);
                rlTextToSpeech = (RelativeLayout) findViewById(R.id.rl_text_to_speech);
                rlLocalSpeech.setOnClickListener(this);
                rlTextToSpeech.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.btn_back:
                                //返回
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter,
                                        R.anim.face_zoom_exit);
                                break;
                        case R.id.btn_complete:
                                //完成
                                if (TextUtils.isEmpty(existPath) && TextUtils.isEmpty(ttsResult)) {
                                        ToastTools.newInstance(this).showText(getString(R.string.no_voice_settings));

                                }
                                else {
                                        //String json=JSON.toJSONString(userQA);
                                        Log.d("SalesQuestionVoiceActiv", existPath + ", ");
                                        if (rlLocalSpeech.isSelected()) {
                                                Intent intent = new Intent();
                                                Bundle b = new Bundle();
                                                b.putString("path", existPath);
                                                b.putInt("type", 0);
                                                intent.putExtra("voice", b);
                                                Log.d("SalesQuestionVoiceActiv", existPath + ", ");
                                                SalesQuestionVoiceActivity.this.setResult(RESULT_OK, intent);
                                                finish();
                                        }
                                        else if (rlTextToSpeech.isSelected()) {
                                                //add(json);
                                                Intent intent = new Intent();
                                                Bundle b = new Bundle();
                                                b.putString("path", ttsResult);
                                                b.putInt("type", 1);
                                                intent.putExtra("voice", b);
                                                Log.d("SalesQuestionVoiceActiv", ttsResult + ", ");
                                                SalesQuestionVoiceActivity.this.setResult(RESULT_OK, intent);
                                                finish();

                                        }
                                }

                                break;
                        case R.id.rl_local_speech:
                                //已有音频
                                startOtherActivity(102, this, ChooseAudioActivity.class);
                                break;
                        case R.id.rl_text_to_speech:
                                //文字转语音
                                startOtherActivity(103, this, TextToSpeechActivity.class);
                                break;
                }
        }

        /**
         * 上传--添加对话
         *
         * @param json
         */
        public void add(String json) {

        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {

                if(resultCode == RESULT_CANCELED){
                        return;
                }
                if ((requestCode == 102 )) {
                        boolean isExist = data.getBooleanExtra("isChecked", false);
                        existPath = data.getStringExtra("path");  //最终所选音频的路径

                        if (isExist) {

                                rlLocalSpeech.setSelected(true);
                                rlTextToSpeech.setSelected(false);
                        }


                }
                else if ((requestCode == 103)) {
                        boolean isExist = data.getBooleanExtra("isChecked", false);
                        ttsResult = data.getStringExtra("ttsResult");    //tts语音内容

                        if (isExist) {

                                rlLocalSpeech.setSelected(false);
                                rlTextToSpeech.setSelected(true);
                        }

                }
        }

        /**
         * 跳转界面
         *
         * @param
         */
        protected void startOtherActivity(int code, Context context, Class<?> cls) {
                Intent intent = new Intent(context, cls);
                if (code == 102) {
                        intent.putExtra("path", "/uusound");
                }
                startActivityForResult(intent, code);
                overridePendingTransition(R.anim.face_zoom_enter,
                        R.anim.face_zoom_exit);
        }


    /*public void addQa(String q){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ApiUtil.HTTP)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        SalesApi salesApi = retrofit.create(SalesApi.class);
        Call<String> stringCall = salesApi.addShoppingQa(q);
        stringCall.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                ToastUtil.show(SalesVoiceActivity.this,"添加问题成功");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                ToastUtil.show(SalesVoiceActivity.this,"添加问题失败");
            }
        });

    }*/

}
